package com.shenyong.ladmin.controller;

import com.shenyong.exception.MyException;
import com.shenyong.ladmin.logConfig.Log;
import com.shenyong.ladmin.logConfig.OperationType;
import com.shenyong.ladmin.result.Page;
import com.shenyong.ladmin.service.UsersService;
import com.shenyong.ladmin.entity.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Create By:qiang
 * Date: 2019/11/25 20:40
 * 文件说明: 后台用户管理
 */
@RestController
public class UsersController {
    @Autowired
    private UsersService usersService;

    /**
     * 分页条件查询
     * @param page
     * @return
     */
    @RequiresAuthentication
    @ApiOperation(value = "查询用户", notes = "分页查询用户", produces = "application/json")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "起始行", required = false, dataType = "int")
            , @ApiImplicitParam(name = "limit", value = "显示多少条数据", required = false, dataType = "int")})
    @RequestMapping("users")
    public Map<String,Object> getAll(Page page){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",usersService.getUsersCount(page));
        map.put("data",usersService.getUsersPage(page));
        return map;
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @RequiresAuthentication
    @Log(describe = "修改用户信息",operationType= OperationType.UPDATE)
    @RequestMapping("upUser")
    public int updUser(Users user){
        int num = usersService.updateUser(user);
        return num;
    }

    /**
     * 查询等级列表
     * @return
     */
    @RequiresAuthentication
    @RequestMapping("findLevelAll")
    public List<Level> findLevelAll(){
        List<Level> levellist=usersService.userLevelAll();
        return levellist;
    }
    @RequiresAuthentication
    @Log(describe = "修改用户状态",operationType= OperationType.UPDATE)
    @RequestMapping("updateUserState")
    public int updateUserState(Users user){
        if(user.getUser_id()==2){
            throw new MyException("超级管理员不可禁用");
        }
        int state = usersService.updateUserStateByUser_Id(user);
        return state;
    }
    @RequiresAuthentication
    @RequestMapping("getAdminUser")
    public Map<String,Object> getAdminUser(Page page){
       return usersService.getAdminUser(page);
    }
    @RequiresAuthentication
    @Log(describe = "添加一个管理员",operationType= OperationType.INSERT)
    @RequestMapping("saveAdminUser")
    public int saveAdminUser(Users users){
        return usersService.saveAdminUser(users);
    }

    @RequestMapping("/getByParent")
    public List<PrivilegeRes> getByParent(){
        return  usersService.getPrivilegeHierarchy();
    }
}
